from django.shortcuts import HttpResponse

# 操作cookie
# 加入cookie
def add_cookie(request):
    response = HttpResponse('add_cookie')
    max_age = 60*60*24*7
    response.set_cookie('name', 'zhangsan',max_age=max_age) # 添加cookie
    return response

# 删除cookie
def delete_cookie(request):
    response = HttpResponse('delete_cookie')
    response.delete_cookie('name') # 删除cookie
    return response

# 获取cookie
def get_cookie(request):
    # 方法一
    # name = request.COOKIES.get('name') # 获取cookie
    # print(name)
    # 方法二
    for key, value in request.COOKIES.items():
        print(key, value)
    return HttpResponse("get cookie")
# 先配置session配置完成，操作session
# 加入session
def add_session(request):
    # 如果没有设置session过期时间，默认是2周后过期
    request.session['name'] = 'zhangsan' # 添加session
    # 如果设置成0，那么浏览器关闭后，session就会过期
    request.session.set_expiry(0) # 设置session过期时间
    return HttpResponse('add_session')

def get_session(request):
    username = request.session.get('name') # 获取session
    print(username)
    return HttpResponse('get session')
